package com.imodia.checkoo.webtag;

import java.io.IOException;
import java.io.Writer;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.hsq.commons.util.BeanFactory;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.imodia.checkoo.bean.CmsArticleBean;
import com.imodia.checkoo.service.CmsArticleService;


public class ArticlePageTag extends RequestContextAwareTag  {
	private static final long serialVersionUID = -1764906844122921796L;
	private TldParam tldParam;
	private PageBean pageParam;

	public TldParam getTldParam() {
		return tldParam;
	}

	public void setTldParam(TldParam tldParam) {
		this.tldParam = tldParam;
	}

	public PageBean getPageParam() {
		return pageParam;
	}

	public void setPageParam(PageBean pageParam) {
		this.pageParam = pageParam;
	}

	@Override
	protected int doStartTagInternal() {
		CmsArticleService cmsArticleService = (CmsArticleService) BeanFactory.getBean("cmsArticleService");
		CmsArticleBean article = new CmsArticleBean();
		article.setDocumentId(tldParam.getDocId());
		article.setCatalogueId(tldParam.getCataId());
		article.setTitle(tldParam.getQueryWord());
		article.setIsValidate(1);
		PageResult<CmsArticleBean> pageList = cmsArticleService.queryForPageBySql(article, pageParam);
		pageContext.setAttribute("ArticlePageTag", pageList);
		
			Writer out = pageContext.getOut();
			StringBuffer buf = new StringBuffer();
			try {
				if (tldParam.getHtmls() != null && tldParam.getHtmls().trim().length() > 0) {
					buf.append(TldUtil.convertArtilce(pageList.getData(), tldParam));
				}
				buf.append("<input name='totalSize' type='hidden' id='totalSize' value='"+pageList.getTotalSize()+"' />");
				buf.append("<input name='pageSize' type='hidden' id='pageSize' value='"+pageList.getPageSize()+"' />");
				buf.append("<input name='pages' type='hidden' id='pages' value='"+pageList.getPages()+"' />");
				buf.append("<input name='currentPage' type='hidden' id='currentPage' value='"+pageList.getCurrentPage()+"' />");
				out.write(buf.toString());
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			out = null;
	
		return EVAL_BODY_INCLUDE;
	}

}
